home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_054 / ispell / lookup.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  2KB  |  83 lines

  1. /* -*- Mode:Text -*- */
  2. /*
  3.  * lookup.c - see if a word appears in the dictionary
  4.  *
  5.  * Pace Willisson, 1983
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. #include "ispell.h"
  11.  
  12.  
  13. struct dent *hashtbl;
  14. int hashsize;
  15. char *hashstrings;
  16. struct dent *lastdent;
  17.  
  18. static inited = 0;
  19.  
  20. linit ()
  21. {
  22.     int hashfd;
  23.     struct hashheader hashheader;
  24.     char hashname[100];
  25.  
  26. #ifdef AMIGA
  27.     strcat (hashname, "ISPELL:ispell.hash");
  28. #else
  29.     strcpy (hashname, LIBDIR);
  30.     strcat (hashname, "/ispell.hash");
  31. #endif
  32.  
  33.     if (inited)
  34.         return;
  35.  
  36.     if ((hashfd = open ("ispell.hash", 0)) < 0 &&
  37.         (hashfd = open (hashname, 0)) < 0) {
  38.         fprintf (stderr, "can't open %s\r\n", hashname);
  39.         return (-1);
  40.     }
  41.  
  42.     read (hashfd, &hashheader, sizeof hashheader);
  43.  
  44.     if (hashheader.magic != MAGIC) {
  45.         fprintf (stderr, "Illegal format hash table\r\n");
  46.         return (-1);
  47.     }
  48.     hashstrings = (char *) malloc (hashheader.stringsize);
  49.     hashtbl = (struct dent *) malloc (hashheader.tblsize * sizeof (struct dent));
  50.     hashsize = hashheader.tblsize;
  51.  
  52.     read (hashfd, hashstrings, hashheader.stringsize);
  53.     read (hashfd, hashtbl, hashheader.tblsize * sizeof (struct dent));
  54.     close (hashfd);
  55.  
  56.     inited = 1;
  57.     return (0);
  58. }
  59.  
  60. /* n is length of s */
  61. struct dent *
  62. lookup (s, n)
  63. register char *s;
  64. {
  65.     register int i;
  66.     register struct dent *dp;
  67.     register char *s1, *s2;
  68.  
  69.     for (i = hash (s, n, hashsize); i > 0; i = (int)(dp->next)) {
  70.         dp = &hashtbl[i];
  71.         /* quick strcmp, but only for equality */
  72.         s1 = &hashstrings [ (int)(dp->word) ];
  73.         s2 = s;
  74.         while (*s1 == *s2++)
  75.             if (*s1++=='\0') {
  76.                 lastdent = &hashtbl[i];
  77.                 return (lastdent);
  78.             }
  79.     }
  80.     return (NULL);
  81. }
  82.  
  83.